/**
 * 产线定义
 */
Ext.define('YX.sbas.Line', {
	  extend : 'Ext.panel.Panel',
	  layout : {
		  type : 'hbox',
		  align : 'stretch'
	  },
	  gridLine : null,
	  sfSearchShopCode : null,
	  txtSearchLineCode : null,
	  cmbSearchLineType : null,
	  frmSearch : null,
	  txtLineCode : null,
	  txtLineDesc : null,
	  sfShopCode : null,
	  cmbLineType : null,
	  txtErpLine : null,
	  sfContacts : null,
	  txtMemo : null,
	  btnSave : null,
	  btnDelete : null,
	  btnSearch : null,
	  btnReset : null,
	  frmOperate : null,
	  require : ['UX.codeview.FieldX'],
	  initComponent : function() {
		  // 车间表格
		  this.gridLine = Ext.create('Ext.grid.Panel', {
			    flex : 1,
			    columns : [{
				      text : T.label.lineCode,
				      dataIndex : 'lineCode'
			      }, {
				      text : T.label.lineDesc,
				      dataIndex : 'lineDesc',
				      width : 160
			      }, {
				      text : T.label.shopCode,
				      dataIndex : 'shopCode'
			      }, {
				      text : T.label.type,
				      dataIndex : 'lineType'
			      }, {
				      text : T.label.erpLine,
				      dataIndex : 'erpLine'
			      }, {
				      text : T.label.contacts,
				      dataIndex : 'contacts'
			      }, {
				      text : T.label.memo,
				      dataIndex : 'memo'
			      }],
			    store : Ext.create('Ext.data.JsonStore', {
				      fields : ['lineCode', 'lineDesc', 'shopCode', 'lineType', 'erpLine',
				        'address', 'contacts', 'memo'],
				      proxy : {
					      type : 'zbusrpc',
					      module : 'SbasViewProductionLineService',
					      reader : {
						      root : 'lineList'
					      }
				      }
			      }),
			    listeners : {
				    scope : this,
				    select : this.onGridLineSelect
			    }
		    });

		  // 查询
		  this.tfSearchShopCode = Ext.widget('codeviewfieldx', {
			    columns : [{
				      text : T.label.shopCode,
				      dataIndex : 'shopCode'
			      }, {
				      text : T.label.shopDesc,
				      dataIndex : 'shopDesc'
			      }],
			    store : Ext.create('Ext.data.JsonStore', {
				      fields : ['shopCode', 'shopDesc'],
				      proxy : {
					      type : 'zbusrpc',
					      module : 'SbasViewWorkshopService',
					      reader : {
						      type : 'json',
						      root : 'shopList'
					      }
				      }
			      }),
			    fieldLabel : T.label.shopCode,
			    labelWidth : 70,
			    anchor : '98%'
		    });
		  this.txtSearchLineCode = Ext.create('Ext.form.field.Text', {
			    fieldLabel : T.label.lineCode,
			    labelWidth : 70,
			    anchor : '98%'
		    });
		  this.cmbSearchLineType = Ext.create('Ext.form.field.ComboBox', {
			    fieldLabel : T.label.type,
			    editable : false,
			    labelWidth : 70,
			    anchor : '98%',
			    valueField : 'key1',
			    displayField : 'data1',
			    store : Ext.create('Ext.data.ArrayStore', {
				      fields : ['key1', 'data1']
			      })
		    });
		  this.frmSearch = Ext.create('Ext.form.Panel', {
			    title : T.label.search,
			    items : [this.sfSearchShopCode, this.txtSearchLineCode,
			      this.cmbSearchLineType]
		    });

		  // 操作
		  this.sfShopCode = Ext.widget('codeviewfieldx', {
			    allowBlank : false,
			    columns : [{
				      text : T.label.shopCode,
				      dataIndex : 'shopCode'
			      }, {
				      text : T.label.shopDesc,
				      dataIndex : 'shopDesc'
			      }],
			    store : Ext.create('Ext.data.JsonStore', {
				      fields : ['shopCode', 'shopDesc'],
				      proxy : {
					      type : 'zbusrpc',
					      module : 'SbasViewWorkshopService',
					      reader : {
						      type : 'json',
						      root : 'shopList'
					      }
				      }
			      }),
			    fieldLabel : T.label.shopCode,
			    labelWidth : 70,
			    anchor : '98%'
		    });
		  this.txtLineCode = Ext.create('Ext.form.field.Text', {
			    fieldLabel : T.label.lineCode,
			    name : 'lineCode',
			    allowBlank : false,
			    labelWidth : 70,
			    anchor : '98%',
			    maxLength : 10
		    });
		  this.txtLineDesc = Ext.create('Ext.form.field.Text', {
			    fieldLabel : T.label.lineDesc,
			    name : 'lineDesc',
			    labelWidth : 70,
			    anchor : '98%',
			    maxLength : 100
		    });
		  this.cmbLineType = Ext.create('Ext.form.field.ComboBox', {
			    fieldLabel : T.label.type,
			    name : 'lineType',
			    labelWidth : 70,
			    anchor : '98%',
			    editable : false,
			    valueField : 'key1',
			    displayField : 'data1',
			    store : Ext.create('Ext.data.JsonStore', {
				      fields : ['key1', 'data1'],
				      proxy : {
					      type : 'zbusrpc',
					      module : 'BasViewCommonDataSetupDetailService',
					      extraParams : {
						      groupName : 'WORKSHOP_TYPE'
					      },
					      reader : {
						      type : 'json',
						      root : 'cdsDtlList'
					      }
				      }
			      })
		    });
		  this.txtErpLine = Ext.create('Ext.form.field.Text', {
			    fieldLabel : T.label.erpLine,
			    name : 'erpLine',
			    labelWidth : 70,
			    anchor : '98%',
			    maxLength : 30
		    });
		  this.sfContacts = Ext.widget('codeviewfieldx', {
			    editable : false,
			    columns : [{
				      text : T.label.account,
				      dataIndex : 'userId'
			      }, {
				      text : T.label.userName,
				      dataIndex : 'userDesc'
			      }],
			    store : Ext.create('Ext.data.JsonStore', {
				      fields : ['userId', 'userDesc'],
				      proxy : {
					      type : 'zbusrpc',
					      module : 'SecViewUserInfoService',
					      reader : {
						      type : 'json',
						      root : 'userList'
					      }
				      }
			      }),
			    fieldLabel : T.label.contacts,
			    labelWidth : 70,
			    anchor : '98%'
		    });
		  this.txtMemo = Ext.create('Ext.form.field.Text', {
			    fieldLabel : T.label.memo,
			    name : 'memo',
			    labelWidth : 70,
			    anchor : '98%',
			    maxLength : 100
		    });

		  this.btnSave = Ext.create('Ext.button.Button', {
			    text : T.label.save,
			    itemId : 'btnSave',
			    iconCls : 'icon-disk',
			    scope : this,
			    handler : this.onBtnSaveClicked
		    });
		  this.btnDelete = Ext.create('Ext.button.Button', {
			    text : T.label.del,
			    itemId : 'btnDelete',
			    iconCls : 'icon-delete',
			    scope : this,
			    handler : this.onBtnDeleteClicked
		    });
		  this.btnSearch = Ext.create('Ext.button.Button', {
			    text : T.label.search,
			    itemId : 'btnSearch',
			    iconCls : 'icon-find',
			    scope : this,
			    handler : this.onBtnSearchClicked
		    });
		  this.btnReset = Ext.create('Ext.button.Button', {
			    text : T.label.reset,
			    itemId : 'btnReset',
			    iconCls : 'icon-clear',
			    scope : this,
			    handler : this.onBtnResetClicked
		    });
		  this.frmOperate = Ext.create('Ext.form.Panel', {
			    title : T.label.operate,
			    flex : 1,
			    bbar : {
				    items : [this.btnSave, this.btnDelete, this.btnSearch, this.btnReset]
			    },
			    items : [this.sfShopCode, this.txtLineCode, this.txtLineDesc,
			      this.cmbLineType, this.txtErpLine, this.sfContacts, this.txtMemo]
		    });

		  this.items = [{
			    flex : 1,
			    layout : {
				    type : 'vbox',
				    align : 'stretch'
			    },
			    items : [this.gridLine],
			    bbar : {
				    xtype : 'pagingtoolbar',
				    displayInfo : true,
				    store : this.gridLine.getStore()
			    }
		    }, {
			    width : 260,
			    layout : {
				    type : 'vbox',
				    align : 'stretch'
			    },
			    items : [this.frmSearch, this.frmOperate]
		    }];

		  this.cmbLineType.getStore().on('load', this.onCmbLineTypeStoreLoad, this);

		  this.callParent();
	  },
	  onGridLineSelect : function(sm, record) {
		  this.frmOperate.loadRecord(record);
	  },
	  onBtnSaveClicked : function(btn) {
		  if (!this.frmOperate.getForm().isValid()) {
			  return;
		  }

		  var me = this;
		  Ext.Msg.show({
			    title : T.label.save,
			    msg : T.msg.are_you_sure_to_save_data,
			    icon : Ext.Msg.QUESTION,
			    buttons : Ext.Msg.YESNO,
			    fn : function(btnId) {
				    if (btnId == 'yes') {
					    btn.disable();

					    YX.ZbusRpc.request({
						      module : 'SbasSaveProductionLineService',
						      showSuccess : true,
						      params : me.frmOperate.getValues(),
						      callback : function() {
							      btn.enable();
						      },
						      success : function() {
							      me.frmOperate.getForm().reset();
							      me.gridLine.getStore().loadPage(1);
						      }
					      });
				    }
			    }
		    });
	  },
	  onBtnDeleteClicked : function(btn) {
		  var rcds = this.gridLine.getSelectionModel().getSelection();
		  if (Ext.isEmpty(rcds)) {
			  return;
		  }

		  var me = this;
		  Ext.Msg.show({
			    title : T.label.del,
			    msg : T.msg.are_you_sure_to_delete_data,
			    icon : Ext.Msg.QUESTION,
			    buttons : Ext.Msg.YESNO,
			    fn : function(btnId) {
				    if (btnId == 'yes') {
					    btn.disable();

					    YX.ZbusRpc.request({
						      module : 'SbasSaveProductionLineService',
						      showSuccess : true,
						      params : {
							      lineCode : rcds[0].get('lineCode'),
							      status : 'D'
						      },
						      callback : function() {
							      btn.enable();
						      },
						      success : function() {
							      me.frmOperate.getForm().reset();
							      me.gridLine.getStore().loadPage(1);
						      }
					      });
				    }
			    }
		    });
	  },
	  onBtnSearchClicked : function(btn) {
		  var vLineCode = this.txtSearchLineCode.getValue();
		  var vLineType = this.cmbSearchLineType.getValue();

		  this.gridLine.getStore().load({
			    params : {
				    lineCode : vLineCode,
				    lineType : vLineType
			    }
		    });
	  },
	  onBtnResetClicked : function(btn) {
		  this.frmOperate.getForm().reset();
	  },
	  onCmbLineTypeStoreLoad : function(store, records) {
		  var searchStore = this.cmbSearchLineType.getStore();
		  searchStore.add({
			    key1 : '',
			    data1 : ''
		    });
		  Ext.each(records, function(rcd) {
			    searchStore.add({
				      key1 : rcd.get('key1'),
				      data1 : rcd.get('data1')
			      });
		    });
	  }
  });